/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.gui.widgets;


// TODO: Set Flags of Items..
public class SPOWidget {

//	public QTreeWidgetItem createUnassignedResults() {
//       if (studentSPO.getUnassignedResults().size() == 0) {
//        	return null;
//        }
//        
//		final QTreeWidgetItem result = new QTreeWidgetItem((QTreeWidgetItem) null);
//        result.setText(0, "Unassigned");
//        for (ExaminationResult r : studentSPO.getUnassignedResults()) {
//            final TaggedTreeWidgetItem<ExaminationResult> item = new TaggedTreeWidgetItem<ExaminationResult>(result);
//            item.setTag(r);
//        	result.addChild(item);
//        	
//        	updateUnassignedResultItem(item);
//        }
//        return result;
//	}
//	
// 
//
//
//	@Override
//	public Qt.ItemFlags flags(final QModelIndex index) 
//	{
//	    final Qt.ItemFlags result = super.flags(index);
//	    result.set(Qt.ItemFlag.ItemIsDropEnabled);
//	    return result;
//	}	
//	
//	protected void removeUnassignedResult(final ExaminationResult r)
//	{
//		studentSPO.removeUnassignedResult(r);
//		studentSPO.getStudent().removeResult(r);
//		
//		final List<QTreeWidgetItem> childs = findItems(r.getCourseIdentifier(), MatchFlag.createQFlags(MatchFlag.MatchExactly, MatchFlag.MatchRecursive));
//		for (QTreeWidgetItem child : childs) {
//			if (child.parent() == unassignedItem) {
//				unassignedItem.removeChild(child);
//				child.dispose();
//			}
//		}
//	}
//	
//
//	
//	
//	protected void dropEventCourse(final QDropEvent event, final Course course)
//	{
////        final String courseId = event.mimeData().data(ProgramDescription.getMime(ExaminationResult.class)).toString();
////        final ExaminationResult source = studentSPO.getUnassignedResult(courseId);
////        if (source == null) {
////        	return ;
////        }
////        
////        if (!course.getName().equals(courseId)) {
////        	final QMessageBox.StandardButtons buttons = new QMessageBox.StandardButtons();
////            buttons.set(QMessageBox.StandardButton.Ok);
////            buttons.set(QMessageBox.StandardButton.Cancel);
////            final QMessageBox.StandardButton ret = QMessageBox.warning(this, tr("Apply grade"),
////                              tr("The course names are not equal. Are you sure to apply the grade?"),
////                                 buttons, QMessageBox.StandardButton.Cancel);
////            
////        	if (ret == QMessageBox.StandardButton.Cancel){
////        		return;
////        	}
////        }
////        
////		final ExaminationResult currentResult = results.get(course);
////		if (currentResult.getGradedResult() == Result.PASSED) {
////        	final QMessageBox.StandardButtons buttons = new QMessageBox.StandardButtons();
////            buttons.set(QMessageBox.StandardButton.Ok);
////            buttons.set(QMessageBox.StandardButton.Cancel);
////            final QMessageBox.StandardButton ret = QMessageBox.warning(this, tr("Apply grade"),
////                              tr("Overwrite grade?"),
////                                 buttons, QMessageBox.StandardButton.Cancel);
////            
////        	if (ret == QMessageBox.StandardButton.Cancel){
////        		return;
////        	}
////		}
////        
////		currentResult.setGrade(source.getGrade());
////		currentResult.setGradedResult(source.getGradedResult());
////		currentResult.setUngradedPassed(source.getUngradedResult());
////        
////        // remove unassigned result
////        removeUnassignedResult(source);
////        
////        event.acceptProposedAction();
//	}
//
//	protected void dropEventModule(final QDropEvent event, final StudentModule module)
//	{
////		final String courseId = event.mimeData().data(ProgramDescription.getMime(ExaminationResult.class)).toString();
////        final ExaminationResult source = studentSPO.getUnassignedResult(courseId);
////        final QTreeWidgetItem parent = this.itemAt(event.pos());
////
////        if (source == null) {
////        	return ;
////        }
////        
////		if (module.getModule().getModuleType() == ModuleType.Module) {
////			// search for a course in that module with the same name 
////            final List<QTreeWidgetItem> items = searchForItem(parent, courseId);
////            if (items.size() != 1) {
////            	QMessageBox.information(this, tr("Information"), tr("No exact matching course"));
////            	return;
////            }
////            
////            final TaggedTreeWidgetItem<?> item = (TaggedTreeWidgetItem<?>)items.get(0);
////            if (!(item.getTag() instanceof Course)) {
////            	// exception
////            	return;
////            }
////            
////            // TODO: Check if there is a grade alread
////            // ask if grade shall be replaced
////            
////            
////            removeUnassignedResult(source);
////			
////            final Course course = (Course)item.getTag();
////            source.setCourseIdentifier(course.getIdentifier());
////            source.setExamNumber(course.getExamNumber());
////
////            studentSPO.getStudent().addResult(source);
////			
////			// update module tree
////			results.put(course, source);
////			recursiveUpdate(item);
////		}
////        
////		if (module.getModule().getModuleType() == ModuleType.IndividualModule) {
////			
////			if (source.getCourseDescription() == null) {
////				// show dialog
////			} else {
////				final Course course = source.getCourseDescription();
////	            
////	            // search for existing result in individual module
////	            final List<QTreeWidgetItem> items = searchForItem(parent, course.getName());
////	            if (items.size() > 1) {
////	            	QMessageBox.information(this, tr("Information"), tr("More than one matching course, please drop on the desired one instead"));
////	            	return;
////	            }
////	
////	            if (items.size() == 1) {
////	            	if (!(items.get(0) instanceof TaggedTreeWidgetItem<?>)) {
////	            		// TODO: won't happen
////	            	}
////	            	final TaggedTreeWidgetItem<?> hit = (TaggedTreeWidgetItem<?>)items.get(0);
////	            	if (!(hit.getTag() instanceof Course)) {
////	            		// TODO: won't happen
////	            	}
////	            	
////	            	final ExaminationResult result = results.get((Course)hit.getTag());
////	            	if (result.getGradedResult() != Result.PASSED) {
////	            		// TODO: Query change
////	            	}
////	            	
////	            	// change item
////	            	
////					removeUnassignedResult(source);
////	
////					result.setGrade(source.getGrade());
////					result.setGradedResult(source.getGradedResult());
////					result.setUngradedPassed(source.getUngradedResult());
////					
////					recursiveUpdate(hit);
////	            } else {
////		            // remove the result
////					removeUnassignedResult(source);
////					
////					final String moduleId = module.getModule().getIdentifier();
////					
////					// update course identifier to correct full qualified id
////					source.setCourseIdentifier(moduleId + "." + course.getName());
////		
////					// reinsert result
////					module.getModule().addCourse(course);
////					studentSPO.getStudent().addResult(source);
////					
////					// update module tree
////					final TaggedTreeWidgetItem<Course> item = createCourseItem(course, source);
////		            parent.addChild(item);
////	            }
////			}
////		}
//	}
//
//	protected void dropEvent(final QDropEvent event)
//    {
////        if (event.source() != this) {
////        	return;
////        }
////    
////        if (event.proposedAction().equals(Qt.DropAction.LinkAction)) {
////
////            final QTreeWidgetItem item = this.itemAt(event.pos());
////            if (!(item instanceof TaggedTreeWidgetItem)) {
////            	return;
////            }
////            
////            final TaggedTreeWidgetItem<?> titem = (TaggedTreeWidgetItem<?>)item;
////            if (titem.getTag() instanceof Course) {
////            	dropEventCourse(event, (Course)titem.getTag());
////            }
////            if (titem.getTag() instanceof StudentModule) {
////            	dropEventModule(event, (StudentModule)titem.getTag());
////            }
////
////            recursiveUpdate(item);
////        } else {
////            return;
////        }
//    }
//
	
	
	
//	public void openAllUnfinished() {
//		final List<QTreeWidgetItem> children = findItems("*", MatchFlag.createQFlags(MatchFlag.MatchWildcard));
//		for (QTreeWidgetItem qo : children) {
//			if (!(qo instanceof TaggedTreeWidgetItem<?>)) {
//				continue;
//			}
//			final TaggedTreeWidgetItem<?> item = (TaggedTreeWidgetItem<?>)qo;
//			
//			if (item.getTag() instanceof StudentModule) {
//				final StudentModule m = (StudentModule)item.getTag();
//				item.setExpanded((m.getECTS(ExaminationResultType.PASSED) < m.getModule().getECTS()));
//			}
//		}
//		
//	}	
//
}
